using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FormInputProject.Models;

namespace FormInputProject.Controllers
{
    public class ChartController : Controller
    {
        private readonly IPointRepository pointRepository;
        private readonly IRecordRepository recordRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public ChartController()
            : this(new PointRepository(), new RecordRepository())
        {
        }

        public ChartController(IPointRepository pointRepository, IRecordRepository recordRepository)
        {
            this.pointRepository = pointRepository;
            this.recordRepository = recordRepository;
        }

        //
        // GET: /Chart/

        public ViewResult Index()
        {
            List<int> point = new List<int>();
            this.ViewData["Point"] = point;
            this.ViewData["start"] = DateTime.Now;
            this.ViewData["end1"] = DateTime.Now;
            this.ViewData["time"] = "";
            var selectList = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId"] = selectList;
            var selectList2 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId2"] = selectList2;
            var selectList3 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId3"] = selectList3;
            var selectList4 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId4"] = selectList4;
            var selectList5 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId5"] = selectList5;

            return View(recordRepository.AllIncluding(record => record.Point));
        }

        [HttpPost]
        public ViewResult Index(FormCollection colloction)
        {
            DateTime start = DateTime.Parse(Request["start"]);
            DateTime end = DateTime.Parse(Request["end"]);
            DateTime start1 = start;
            DateTime end1 = end;
            string time = "[";
            List<int> point = new List<int>();
            if (Request["PointId"].ToString() != "")
            {
                point.Add(int.Parse(Request["PointId"].ToString()));
            }
            if (Request["PointId2"].ToString() != "")
            {
                point.Add(int.Parse(Request["PointId2"].ToString()));
            }
            var query = from Record r in recordRepository.All
                        where r.WriteTime >= start && r.WriteTime <= end && point.Contains(r.PointId)
                        orderby r.WriteTime
                        select r;

            while (start1 <= end1)
            {
                time += start1.Day.ToString() + ",";
                start1 = start1.AddDays(1);
            }
            time = time.Remove(time.Length - 1);
            time += "]";

            this.ViewData["Point"] = point;
            this.ViewData["start"] = start;
            this.ViewData["end1"] = end;
            this.ViewData["time"] = time;
            var selectList = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId"] = selectList;
            var selectList2 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId2"] = selectList2;
            var selectList3 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId3"] = selectList3;
            var selectList4 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId4"] = selectList4;
            var selectList5 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId5"] = selectList5;


            return View(query.ToList());
        }

        //
        // GET: /Chart/Details/5

        public ViewResult Details(int id)
        {
            return View(recordRepository.Find(id));
        }

        //
        // GET: /Chart/Create

        public ActionResult Create()
        {
            List<int> point = new List<int>();
            this.ViewData["Point"] = point;
            this.ViewData["start"] = DateTime.Now;
            this.ViewData["end1"] = DateTime.Now;
            this.ViewData["time"] = "";
            var selectList = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId"] = selectList;
            var selectList2 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId2"] = selectList2;
            var selectList3 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId3"] = selectList3;
            var selectList4 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId4"] = selectList4;
            var selectList5 = new SelectList(pointRepository.All, "PointId", "PointName");
            this.ViewData["PointId5"] = selectList5;

            ViewBag.PossiblePoint = pointRepository.All;
            return View();
        }

        //
        // POST: /Chart/Create

        [HttpPost]
        public ActionResult Create(Record record)
        {
            if (ModelState.IsValid)
            {
                recordRepository.InsertOrUpdate(record);
                recordRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                DateTime start = DateTime.Parse(Request["start"]);
                DateTime start1 = start;
                string time = "[";
                List<int> point = new List<int>();
                if (Request["PointId"].ToString() != "")
                {
                    point.Add(int.Parse(Request["PointId"].ToString()));
                }
                if (Request["PointId2"].ToString() != "")
                {
                    point.Add(int.Parse(Request["PointId2"].ToString()));
                }
                var query = from Record r in recordRepository.All
                            where r.WriteTime == start  && point.Contains(r.PointId)
                            orderby r.WriteTime
                            select r;

                this.ViewData["Point"] = point;
                this.ViewData["start"] = start;
                this.ViewData["time"] = time;
                var selectList = new SelectList(pointRepository.All, "PointId", "PointName");
                this.ViewData["PointId"] = selectList;
                var selectList2 = new SelectList(pointRepository.All, "PointId", "PointName");
                this.ViewData["PointId2"] = selectList2;
                var selectList3 = new SelectList(pointRepository.All, "PointId", "PointName");
                this.ViewData["PointId3"] = selectList3;
                var selectList4 = new SelectList(pointRepository.All, "PointId", "PointName");
                this.ViewData["PointId4"] = selectList4;
                var selectList5 = new SelectList(pointRepository.All, "PointId", "PointName");
                this.ViewData["PointId5"] = selectList5;

                ViewBag.PossiblePoint = pointRepository.All;
                return View();
            }
        }

        //
        // GET: /Chart/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.PossiblePoint = pointRepository.All;
            return View(recordRepository.Find(id));
        }

        //
        // POST: /Chart/Edit/5

        [HttpPost]
        public ActionResult Edit(Record record)
        {
            if (ModelState.IsValid)
            {
                recordRepository.InsertOrUpdate(record);
                recordRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.PossiblePoint = pointRepository.All;
                return View();
            }
        }

        //
        // GET: /Chart/Delete/5

        public ActionResult Delete(int id)
        {
            return View(recordRepository.Find(id));
        }

        //
        // POST: /Chart/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            recordRepository.Delete(id);
            recordRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                pointRepository.Dispose();
                recordRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

